package loginview;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import static users.LoginFun.addUser;
import static users.LoginFun.showJDialog;

public class AddMessage extends JFrame {
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
    JPanel p3=new JPanel();
    JPanel p4=new JPanel();
    JPanel p5=new JPanel();
    JPanel p6=new JPanel();

    JLabel welcome=new JLabel("完善信息");
    JLabel name=new JLabel("姓名:     ");
    JLabel gender=new JLabel("性别:     ");
    JLabel phone=new JLabel("电话号码: ");
    JLabel address=new JLabel("地址:     ");

    JTextField nameText=new JTextField(11);
    JTextField genderText=new JTextField(11);
    JTextField phoneText=new JTextField(11);
    JTextField addressText=new JTextField(11);

    JButton becomeUser=new JButton("注册成普通用户");
    JButton becomeAdmin=new JButton("注册成管理员");
    String username;
    String password;

    public AddMessage(String username,String password){
        this.username=username;
        this.password=password;
        //初始化界面
        initJFrame();
        //在这个界面添加内容
        initView();
        //让页面展示出来
        this.setVisible(true);

    }

    private void initJFrame() {
        this.setTitle("完善信息");
        this.setSize(500, 400);
        this.setLayout(new GridLayout(7, 1,10,10));
        //设置页面置顶
        //this.setAlwaysOnTop(true);
        //设置页面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void initView() {
        welcome.setFont(new Font("微软雅黑", Font.BOLD, 20));
        Font size=new Font("微软雅黑", Font.PLAIN, 15);
        name.setFont(size);
        gender.setFont(size);
        phone.setFont(size);
        address.setFont(size);
        p1.add(welcome);
        p2.add(name);
        p2.add(nameText);
        p3.add(gender);
        p3.add(genderText);
        p4.add(phone);
        p4.add(phoneText);
        p5.add(address);
        p5.add(addressText);
        p6.add(becomeUser);
        p6.add(becomeAdmin);

        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        this.add(p5);
        this.add(p6);

        //注册成用户按钮监听
        becomeUser.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //判断姓名、性别、电话、地址输入是否为空
                if(nameText.getText().isEmpty()||genderText.getText().isEmpty()||phoneText.getText().isEmpty()||addressText.getText().isEmpty()){
                    showJDialog("信息未填完，请完善！");
                    return;
                }
                try {
                    addUser(username,password,nameText.getText(),genderText.getText(), phoneText.getText(),addressText.getText(),"0");
                    AddMessage.this.setVisible(false);
                    new Login();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        //注册成管理员按钮监听
        becomeAdmin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //判断姓名、性别、电话、地址输入是否为空
                if(nameText.getText().isEmpty()||genderText.getText().isEmpty()||phoneText.getText().isEmpty()||addressText.getText().isEmpty()){
                    showJDialog("信息未填完，请完善！");
                    return;
                }
                try {
                    addUser(username,password,nameText.getText(),genderText.getText(),phoneText.getText(),addressText.getText(),"-1");
                    AddMessage.this.setVisible(false);
                    new Login();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }
}
